import java.util.*;

public class StudentSheet {
    ArrayList<Student> arr=new ArrayList<>();
    /*
    -----------------------------获取student的数量-------------
     */
    public void getNum(ArrayList<Student> arr){
        System.out.println("学生的数量为："+arr.size());
    }
    /*
    -------------------------------添加学生-----------------
     */
    public void addStu(ArrayList<Student> arr) {
        System.out.println("----请输入学生信息---");
        System.out.print("姓名：");
        Scanner s = new Scanner(System.in);

        String name = s.next();
        while (true) {
            if (name.length() < 1) {
                System.out.print("姓名不能为空，请重新输入：");
                name = s.next();
            } else
                break;
        }

        System.out.print("性别：");
        String sex = s.next();
        while (true) {
            if (sex.compareTo("男") == 0 || sex.compareTo("女") == 0) {
                break;
            } else {
                System.out.println("请输入性别 男/女：");
                sex = s.next();
            }

        }
        System.out.print("学号：");
        int flag = 0;
        String ID = s.next();
        while (true) {
            flag=0;
            if (ID.length() < 1) {
                System.out.println("学号不能为空：");

            }
            else {
                for (int i = 0; i <= arr.size() - 1; i++) {
                    Student student = arr.get(i);
                    if (student.getID().equals(ID)) {
                        System.out.println("学号存在，请重新输入学号：");
                        flag = 1;
                        break;
                    }

                }
                if (flag == 0)
                    break;
           }
            ID=s.next();
        }
            System.out.print("年龄：");
            int old = s.nextInt();
            while (true) {
                if (old >= 0) {
                    break;
                } else {
                    System.out.println("请重新输入年龄：");
                }
            }
            System.out.print("成绩：");
            float score = s.nextFloat();
            Student student = new Student(name, sex, ID, old, score);

            arr.add(student);
            System.out.println("----------添加成功---------");

    }

    /*
    ----------------------------按学号删除信息=--------------------
     */
    public void delete(ArrayList<Student> arr){
        System.out.print("请输入要删除学生信息的学号：");
        Scanner scanner=new Scanner(System.in);
        String ID;
        int flag=0;
        while(true) {
            flag=0;
            ID = scanner.next();
//            if (ID.length() < 1) {
//                System.out.println("学号输入错误，请重新输入学号：");
//                ID = scanner.next();
//            }
            for (int i=0;i<=arr.size()-1;i++){
                Student student= arr.get(i);
                if (student.getID().equals(ID)){
                    arr.remove(student);
                    System.out.println("ID:"+student.getID()+"已删除该信息");
                    flag=1;
                    break;
                }
            }
            if (flag==1){
                break;
            }
            else
                System.out.println("没有该学号，请重新输入学号：");
        }
    }

    /*
    ------------------------------按学号查找并返回一个学生-------------------
     */
    public Student IDback(ArrayList<Student> arr){
        System.out.println("请输入要查找学生信息的学号");
        Scanner scanner=new Scanner(System.in);

        String ID=scanner.next();
        while(true){
            if (ID.length()<1){
                System.out.println("学号不能为空");
            }
            else {
                for (int i = 0; i <arr.size() ; i++) {
                    Student student = new Student();
                    student = arr.get(i);
                    if (ID.equals(student.getID())) {
                        return student;
                    }

                }
            }
            System.out.println("学号不存在，请重新输入学号：");
            ID=scanner.next();
        }
    }

    /*
    ------------------------------按姓名查找并返回学生-------------

    姓名可能为多个
     */
    public Student Nameback( ArrayList<Student> arr){
        System.out.println("请输入要查找学生信息的姓名");
        Scanner scanner=new Scanner(System.in);

        String name= scanner.next();
        while(true){
            if (name.equals(null)){
                System.out.println("姓名不能为空");
            }
            else {
                Student student = new Student();
                for (int i = 0; i < arr.size() ; i++) {
                    student = arr.get(i);
                    if (student.getName().equals(name)) {
                        return student;
                    }
                }

            }
            System.out.println("学号不存在，请重新输入学号：");
            name=scanner.next();
        }
    }

    /*
    ----------------------------------按绩点排序（倒序）返回学生信息list
     */
    public List <Student>getStuInfoList(ArrayList<Student> arr){
        Collections.sort(arr, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getScore()<o2.getScore()) {
                    return 1;
                }
                if (o1.getScore()==o2.getScore()){
                    return 0;
                }
               return -1;
            }
        });
        System.out.format("%-8s%-8s%-8s\n","ID","name","sex");
        for (int i = 0; i <arr.size() ; i++) {
            arr.get(i).show();
        }
        return arr;
    }

    /*
    -----------------------------返回字符串形式的学生名单----------------
     */
    public String Show(ArrayList<Student> arr){
       //return  arr.toString();
//        return for(int i=0;i<arr.size();i++){
//            System.out.println(arr.get(i).toString());
//        }
        System.out.format("%-8s%-8s%-4s","ID","name","sex\n");
        return arr.toString();
    }

}
